package com.idanchuang.component.logback.appender;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.CallerData;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.LoggerContextVO;
import com.idanchuang.component.logback.converter.DurationPatternConverter;
import com.idanchuang.component.logback.converter.KeywordPatternConverter;
import com.idanchuang.component.logback.converter.TraceIdPatternConverter;
import com.idanchuang.component.logback.converter.UrlPatternConverter;
import org.slf4j.Marker;

import java.util.Map;

/**
 * @author yjy
 * Created at 2021/8/24 5:46 下午
 */
public class DcILoggingEvent implements ILoggingEvent {

    private final ILoggingEvent iLoggingEvent;

    private final String traceId;
    private final String file;
    private final String line;
    private final String method;
    private final String url;
    private final String duration;
    private final String keyword;

    public DcILoggingEvent(ILoggingEvent iLoggingEvent) {
        this.keyword = KeywordPatternConverter.figure(iLoggingEvent);
        this.traceId = TraceIdPatternConverter.figure(iLoggingEvent);
        StackTraceElement[] cda = iLoggingEvent.getCallerData();
        if (cda != null && cda.length > 0) {
            this.line = Integer.toString(cda[0].getLineNumber());
            this.file = cda[0].getFileName();
            this.method = cda[0].getMethodName();
        } else {
            this.line = CallerData.NA;
            this.file = CallerData.NA;
            this.method = CallerData.NA;
        }
        this.url = UrlPatternConverter.figure(iLoggingEvent);
        this.duration = DurationPatternConverter.figure(iLoggingEvent);
        this.iLoggingEvent = iLoggingEvent;
    }

    @Override
    public String getThreadName() {
        return this.iLoggingEvent.getThreadName();
    }

    @Override
    public Level getLevel() {
        return this.iLoggingEvent.getLevel();
    }

    @Override
    public String getMessage() {
        return this.iLoggingEvent.getMessage();
    }

    @Override
    public Object[] getArgumentArray() {
        return this.iLoggingEvent.getArgumentArray();
    }

    @Override
    public String getFormattedMessage() {
        return this.iLoggingEvent.getFormattedMessage();
    }

    public String getKeyword() {
        return keyword;
    }

    @Override
    public String getLoggerName() {
        return this.iLoggingEvent.getLoggerName();
    }

    @Override
    public LoggerContextVO getLoggerContextVO() {
        return this.iLoggingEvent.getLoggerContextVO();
    }

    @Override
    public IThrowableProxy getThrowableProxy() {
        return this.iLoggingEvent.getThrowableProxy();
    }

    public String getUrl() {
        return url;
    }

    public String getDuration() {
        return duration;
    }

    @Override
    public StackTraceElement[] getCallerData() {
        return this.iLoggingEvent.getCallerData();
    }

    @Override
    public boolean hasCallerData() {
        return this.iLoggingEvent.hasCallerData();
    }

    @Override
    public Marker getMarker() {
        return this.iLoggingEvent.getMarker();
    }

    @Override
    public Map<String, String> getMDCPropertyMap() {
        return this.iLoggingEvent.getMDCPropertyMap();
    }

    @Override
    public Map<String, String> getMdc() {
        return this.iLoggingEvent.getMdc();
    }

    @Override
    public long getTimeStamp() {
        return this.iLoggingEvent.getTimeStamp();
    }

    @Override
    public void prepareForDeferredProcessing() {
        this.iLoggingEvent.prepareForDeferredProcessing();
    }

    public String getTraceId() {
        return traceId;
    }

    public String getFile() {
        return file;
    }

    public String getLine() {
        return line;
    }

    public String getMethod() {
        return method;
    }

    public ILoggingEvent getiLoggingEvent() {
        return iLoggingEvent;
    }
}
